<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Menu extends Front_Controller{

	function Menu(){
        parent::Front_Controller();
	}	
	function index(){
        $CI = &get_instance(); 
		$page_name = $CI->uri->segment(1);        
		$page_name = $page_name?$page_name:'Trang-Chu';
		$str = "";
		$select = 'SELECT n.'.NAVIGATION_MENU_ID.', n.'.NAVIGATION_NAME.', n.'.NAVIGATION_TOPIC.', n.'.NAVIGATION_PAR_MENU_ID.', '.NAVIGATION_URL.', n.'.NAVIGATION_MENU_ID.', n.'.NAVIGATION_PID.', p.'.PAGE_VISIBLE;
		$from = ' FROM '.NAVIGATION.' n LEFT JOIN '.PAGE. ' p  ON n.'.NAVIGATION_PID.'='.'p.'.PAGE_PAGE_ID;
        $order = ' order by n.'.NAVIGATION_SUB_MENU_ID.' ';
        $cond = ' WHERE n.'.NAVIGATION_ITEM_VISIBLE.' = 1 ';
        $cond .= ' AND (n.'.NAVIGATION_PAR_MENU_ID.' IS NULL OR n.'.NAVIGATION_PAR_MENU_ID.' = 0 )';		
		$sql = $select.$from.$cond.$order;		
        $results = $CI->db->query($sql)->result();		
		for($i=0;$i<count($results);$i++){
			$class = "";
			//echo $results[$i]->{NAVIGATION_URL}.'=='.$page_name;
			if($results[$i]->{NAVIGATION_URL}==$page_name){
				$class = "active";
			}
			$str .= "<li><a  class = '".$class."' href='".site_url($results[$i]->{NAVIGATION_URL})."' title='".$results[$i]->{NAVIGATION_NAME}."'><strong>".($results[$i]->{NAVIGATION_NAME})."</strong></a></li>";
			
		}
		$this->add('results', $results);
		$this->add('menu', $str);
		
	}
	
	function index_old(){
        $CI = &get_instance();
        $fid = $CI->input->get('fid');        
		$str = "";
		$select = 'SELECT n.'.NAVIGATION_MENU_ID.', n.'.NAVIGATION_NAME.', n.'.NAVIGATION_TOPIC.', n.'.NAVIGATION_PAR_MENU_ID.', '.NAVIGATION_URL.', n.'.NAVIGATION_MENU_ID.', n.'.NAVIGATION_PID.', p.'.PAGE_VISIBLE;
		$from = ' FROM '.NAVIGATION.' n LEFT JOIN '.PAGE. ' p  ON n.'.NAVIGATION_PID.'='.'p.'.PAGE_PAGE_ID;
        $order = ' order by n.'.NAVIGATION_SUB_MENU_ID.' ';
        $cond = ' WHERE n.'.NAVIGATION_ITEM_VISIBLE.' = 1 ';
        $cond .= ' AND (n.'.NAVIGATION_PAR_MENU_ID.' IS NULL OR n.'.NAVIGATION_PAR_MENU_ID.' = 0 )';		
		$sql = $select.$from.$cond.$order;		
        $results = $CI->db->query($sql)->result();		
		
		for($i=0;$i<count($results);$i++){
			if($results[$i]->{PAGE_VISIBLE}==1)
				$link = site_url()."?fid=".$fid."&pid=".$results[$i]->{NAVIGATION_PID};
			else
				$link = "#";
            $w = 0;
            if(strlen($results[$i]->{NAVIGATION_NAME}) > 13){
                $w = strlen($results[$i]->{NAVIGATION_NAME})*7;
            }
            if($w)
                $str .= "<li class='top'><a style='width:".$w."px' href=".$link." class='na'><nobr><b>".$results[$i]->{NAVIGATION_NAME}."</b></nobr></a>";
            else
                $str .= "<li class='top'><a href=".$link." class='na'><nobr><b>".$results[$i]->{NAVIGATION_NAME}."</b></nobr></a>";			
			$this->get_recursive($results[$i]->{NAVIGATION_MENU_ID}, $str);
			$str .="</li>";

			
		}
				
		$this->add('results', $results);
		$this->add('menu', $str);
        $this->add('admin_id', $fid);
		
	}
	
	function get_recursive($parent_id, &$str){
        global $CI;
		$fid = $CI->input->get('fid');        	
		$select = 'SELECT n.'.NAVIGATION_MENU_ID.', n.'.NAVIGATION_NAME.', n.'.NAVIGATION_TOPIC.', n.'.NAVIGATION_PAR_MENU_ID.', '.NAVIGATION_URL.', n.'.NAVIGATION_MENU_ID.', n.'.NAVIGATION_PID.', p.'.PAGE_VISIBLE;
		$from = ' FROM '.NAVIGATION.' n LEFT JOIN '.PAGE. ' p  ON n.'.NAVIGATION_PID.'='.'p.'.PAGE_PAGE_ID;
        $order = ' order by n.'.NAVIGATION_SUB_MENU_ID.' ';
        $cond = ' WHERE n.'.NAVIGATION_ITEM_VISIBLE.' = 1 ';
        $cond .= ' AND n.'.NAVIGATION_PAR_MENU_ID.' = '.$parent_id;
		$sql = $select.$from.$cond.$order;		
        $results = $CI->db->query($sql)->result();	       
        
		if(!empty($results)){
            $str .= "<ul>";
            foreach($results as $row){      
				if($row->{PAGE_VISIBLE}==1)
					$link = site_url()."?fid=".$fid."&pid=".$row->{NAVIGATION_PID};
				else
					$link = "#";
				$str .= "<li  class='sub_li' onmouseover=\"this.className='sub_li_hover'\" onmouseout=\"this.className='sub_li'\"><a href=".$link."><nobr>".$row->{NAVIGATION_NAME}."</nobr></a>";			
				$this->get_recursive($row->{NAVIGATION_MENU_ID}, $str);
				$str .="</li>";
            }			
            $str .= "</ul>";
        }
	
    }
	
}